package com.scenery.server.services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.scenery.server.vo.DianPing;

public class DianPingServices extends Manager{
	/***
	 * 添加点评
	 * **/
	public static int addDianPing(String userId,String sceneryId,String text)
	{
		Connection conn = null;
		PreparedStatement ps = null;
		
		String sql = "insert into  lv_dianping ( `sceneryId`, `userId`,  `pingjia`,`dptime`) VALUES  (?,?,?,?) ";
		int ret=1;
		String pwd="";
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();

		SimpleDateFormat ymdhmsf = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String reqTime = ymdhmsf.format(date);
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, sceneryId);
				ps.setString(2,userId);
				ps.setString(3, text);
				
				ps.setString(4, reqTime);
				ps.executeUpdate();
				ps.close();
				conn.close();
				ret=0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=3;
			}

		}
		return ret;
	}
	/**
	 * 查询点评
	 * **/
	public static List<DianPing>  select(String sceneryId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select  `userId`, `sceneryId`,  `pingjia`,`dptime` from  lv_dianping";
		int ret=1;
		String pwd="";
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		List<DianPing> list=new ArrayList<DianPing>();
		SimpleDateFormat ymdhmsf = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String reqTime = ymdhmsf.format(date);
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				rs=ps.executeQuery();
				while(rs.next())
				{
					DianPing bean=new DianPing(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4));
					
					list.add(bean);
				}
				rs.close();
				ps.close();
				conn.close();
				ret=0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=3;
			}

		}
		return list;
	}
	public static void main(String args[]){
		//System.out.println(p(""));
		//p(addDianPing("9739","aaa","一般"));
		p(select("").size());
	}
	
}
